package com.xm.xmapp.fragment;

import java.util.ArrayList;

import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.VolleyError;
import com.xm.xmapp.MainActivity;
import com.xm.xmapp.MessageDetailsActivity;
import com.xm.xmapp.R;
import com.xm.xmapp.adapter.SystemMessageAdapter;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.customview.XListView;
import com.xm.xmapp.customview.XListView.IXListViewListener;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.ListViewOnclickInterFace;
import com.xm.xmapp.request.bean.Get_system_message_listRequest;
import com.xm.xmapp.response.bean.BaseListResponse;
import com.xm.xmapp.response.bean.ResponseAllMessageId;
import com.xm.xmapp.response.bean.ResponseSystemMessageListBean;
import com.xm.xmapp.tools.Helper_SharedPreferences;
import com.xm.xmapp.tools.MyCache;

public class MessageSystemFragment extends BaseFragment {

	private XListView xListView;
	private RelativeLayout rel;
	private Get_system_message_listRequest requestBean;
	private ArrayList<ResponseSystemMessageListBean> list;
	private SystemMessageAdapter adapter;
	private GetDataInterFace<String> listener;

	public MessageSystemFragment() {
	}

	@Override
	public int setContentView() {
		return R.layout.fragment_systemmessage;
	}

	@Override
	public void initView() {
		xListView = (XListView) findViewById(R.id.xListView);
		rel = (RelativeLayout) findViewById(R.id.rel);
	}

	@Override
	public void initData() {
		requestBean = new Get_system_message_listRequest();
		list = new ArrayList<ResponseSystemMessageListBean>();
		adapter = new SystemMessageAdapter(list, getActivity(), R.layout.item_systemmessage);
		xListView.setAdapter(adapter);
		adapter.setIn(new ListViewOnclickInterFace() {

			@Override
			public Object DoSomeThing(Object data) {

				ResponseSystemMessageListBean t = (ResponseSystemMessageListBean) data;
				Intent intent = new Intent(MyAppcation.getMyAppcation(), MessageDetailsActivity.class);
				intent.putExtra("id", t.system_message_id);
				startActivity(intent);
				return null;
			}
		});

		xListView.setPullRefreshEnable(true);
		xListView.setPullLoadEnable(true);
		promptDialog.show();
		ResponseAllMessageId bean1 = (ResponseAllMessageId) Helper_SharedPreferences
				.get_obj_sp(CodeConstant.messageClient);

		ResponseAllMessageId bean2 = (ResponseAllMessageId) Helper_SharedPreferences
				.get_obj_sp(CodeConstant.messageServer);

		if (bean1 == null)
			bean1 = new ResponseAllMessageId();
		if (bean1 != null && bean2 != null) {
			bean1.system_message_max_id = bean2.system_message_max_id;
			Helper_SharedPreferences.set_obj_sp(CodeConstant.messageClient, bean1);
			MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class, Type.A_Main.MSG_CODE, 0);
		}
		requestBean.system_message_id = Helper_SharedPreferences.get_int_sp(CodeConstant.Max_system_message_id);
		requestBean.type = 1;
		listener = new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				xListView.stopRefresh();
				xListView.stopLoadMore();
				BaseListResponse<ResponseSystemMessageListBean> base = JSON.parseObject(response,
						new TypeReference<BaseListResponse<ResponseSystemMessageListBean>>() {
						});

				if (base.list == null || base.list.size() == 0) {
					xListView.setPullLoadEnable(false);
				} else {
					if (requestBean.type == 1)
						list.clear();
					list.addAll(base.list);
					if (Helper_SharedPreferences.get_int_sp(CodeConstant.Max_system_message_id) < Integer.parseInt(list
							.get(0).system_message_id))
						Helper_SharedPreferences.set_int_sp(CodeConstant.Max_system_message_id,
								Integer.parseInt(list.get(0).system_message_id));

					Helper_SharedPreferences.set_int_sp(CodeConstant.Min_system_message_id,
							Integer.parseInt(list.get(list.size() - 1).system_message_id));

				}

				if (list.size() == 0) {
					rel.setVisibility(View.VISIBLE);
					xListView.setVisibility(View.GONE);
				} else {
					rel.setVisibility(View.GONE);
					xListView.setVisibility(View.VISIBLE);
				}
				adapter.notifyDataSetChanged();
			}
		};

		if (!getMyCache())
			getData();

	}

	private boolean getMyCache() {
		String response = MyCache.getMyCache().getCache(requestBean.getCacheString());
		if (response != null && !response.equals("")) {
			listener.onResponse(response);
			return true;
		}
		Helper_SharedPreferences.set_int_sp(CodeConstant.Max_system_message_id, 0);
		requestBean.system_message_id = 0;
		return false;
	}

	private void getData() {

		MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, listener, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				xListView.stopRefresh();
				xListView.stopLoadMore();
				promptDialog.dismiss();
			}
		});

	}

	@Override
	public void initEvent() {
		xListView.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				requestBean.type = 1;
				requestBean.system_message_id = Helper_SharedPreferences.get_int_sp(CodeConstant.Max_system_message_id);
				xListView.setPullLoadEnable(true);
				getData();
			}

			@Override
			public void onLoadMore() {
				requestBean.type = 0;
				requestBean.system_message_id = list.get(list.size() - 1).system_message_id;
				getData();
			}
		});
	}

}
